#include <portability/x3port.h>
#include <iostream>
using namespace std;

int test();

#define  LOADMODE 2

#if LOADMODE==1
#include <nonplugin/useplugins.h>
int main()
{
    const char* plugins[] =
    {
        "x3manager.pln",
        "plugin_a.pln",
        "plugin_b.pln",
        "plugin_c.pln",
        NULL
    };

    x3::AutoLoadPlugins autoload(plugins, "plugins");

    return test();
}

#elif LOADMODE==2
#include <nonplugin/scanplugins.h>
int main(int argc, char *argv[])
{
    /* x3::loadScanPlugins("D:/workspace/git/x3py-demo/lib/cyg/middle-plugins"); */
    x3::loadScanPlugins();

    int ret = test();

    x3::unloadScanPlugins();

    return ret;
}
#endif

#include "iplugin_a.h"
#include "iplugin_b.h"
#include "iplugin_c.h"
#include "iplugin_d.h"
#include "manager/iplugins.h"

int test()
{
    x3::Object<x3::IPlugins> x3m1(x3::clsidManager);
    std::vector<std::string> files;
    x3m1->getPluginFiles(files);

    x3::Object<x3::IPlugins> x3m2(x3::clsidManager);
    x3m2->getPluginCount();

    x3::Object<iPluginA> pa1(clsid_a), pa2(clsid_a);
    if (pa1)
    {
        printf("The plugin is loaded (%s in %s).\n",
               pa1->getInterfaceName(), pa1->getClassName());
    }
    else
    {
        printf("The plugin is not loaded.\n");
    }
    printf("pa1: 0x%p, %d, pa2: 0x%p, %d\n\n", &pa1, pa1->add(1, 2), &pa2, pa2->add(3, 4));

    x3::Object<iPluginB> pb(clsid_b);
    if (pb)
    {
        printf("The plugin is loaded (%s in %s).\n",
               pb->getInterfaceName(), pb->getClassName());
    }
    else
    {
        printf("The plugin is not loaded.\n");
    }
    printf("pb: 0x%p, %d\n\n", &pb, pb->sum(1, 2));

    x3::Object<iPluginC> pc(clsid_c);
    pc->call_a_b();

    x3::Object<iPluginD> pd1(clsid_d), pd2(clsid_d);
    if (pd1)
    {
        printf("The plugin is loaded (%s in %s).\n",
               pd1->getInterfaceName(), pd1->getClassName());
    }
    else
    {
        printf("The plugin is not loaded.\n");
    }

    printf("pd1: 0x%p, %d, pd2: 0x%p, %d\n", &pd1, pd1->add(10, 20), &pd2, pd2->add(20, 40));
    getchar();

    return 0;
}
